/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateProduct2.java
 *
 * Created on Dec 10, 2009, 11:02:06 AM
 */

package GUI;

import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author trangnn_b00557
 */
public class CreateProduct extends javax.swing.JDialog {

    /** Creates new form CreateProduct2 */
    public CreateProduct(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtExpiryDate = new javax.swing.JTextField();
        lblType = new javax.swing.JLabel();
        lbl = new javax.swing.JLabel();
        lblProductionDate = new javax.swing.JLabel();
        txtImportDate = new javax.swing.JTextField();
        lblDecription = new javax.swing.JLabel();
        lblExpiryDate = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtDscription = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        lblPrice = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        txtProductionDate = new javax.swing.JTextField();
        btRefresh = new javax.swing.JButton();
        btExit = new javax.swing.JButton();
        lblName = new javax.swing.JLabel();
        btCreate = new javax.swing.JButton();
        btRefreshOrder = new javax.swing.JButton();
        txtType = new javax.swing.JTextField();
        lblPrice1 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        lblPrice2 = new javax.swing.JLabel();
        txtTax = new javax.swing.JTextField();
        lblPrice3 = new javax.swing.JLabel();
        txtSellPrice = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Create Product", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Monotype Corsiva", 1, 36), new java.awt.Color(51, 102, 0))); // NOI18N

        lblType.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblType.setText("Type : ");

        lbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbl.setText("Import Date : ");

        lblProductionDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblProductionDate.setText("Production Date : ");

        lblDecription.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblDecription.setText("Description : ");

        lblExpiryDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblExpiryDate.setText("Expiry Date : ");

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        txtDscription.setViewportView(txtDescription);

        lblPrice.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice.setText(" Price : ");

        btRefresh.setText("Refresh");

        btExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btExit.setText("Close");
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitActionPerformed(evt);
            }
        });

        lblName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblName.setText("Name : ");

        btCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btCreate.setText("Create");
        btCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCreateActionPerformed(evt);
            }
        });

        btRefreshOrder.setText("Refresh");
        btRefreshOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRefreshOrderActionPerformed(evt);
            }
        });

        lblPrice1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice1.setText("Quantity:");

        lblPrice2.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice2.setText("Tax :");

        lblPrice3.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice3.setText("Sell Price:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblType)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblExpiryDate)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lbl)
                                .addComponent(lblPrice)
                                .addComponent(lblPrice2)
                                .addComponent(lblPrice1)
                                .addComponent(lblPrice3)
                                .addComponent(lblDecription))))
                    .addComponent(lblName)
                    .addComponent(lblProductionDate))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btCreate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRefreshOrder)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btExit))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtDscription, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtPrice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtImportDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtExpiryDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtProductionDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtType, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtTax, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtQuantity, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(txtSellPrice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                        .addGap(34, 34, 34)))
                .addGap(71, 71, 71))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblProductionDate)
                    .addComponent(txtProductionDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblExpiryDate)
                    .addComponent(txtExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbl)
                    .addComponent(txtImportDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPrice2)
                            .addComponent(txtTax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice1))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSellPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice3))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(txtDscription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btRefreshOrder)
                            .addComponent(btExit)
                            .addComponent(btCreate)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lblDecription)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btExitActionPerformed

    private void btCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateActionPerformed
        // TODO add your handling code here:
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        if (txtName.getText().trim().equals("")) {
        JOptionPane.showMessageDialog(this, "Please enter product name");
        return;
        }
        if (txtType.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter Type");
            return;
        }
        if (txtProductionDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtProductionDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format MM/DD/YYYY");
            return;
        }

        if (txtExpiryDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtExpiryDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format MM/DD/YYYY");
            return;
        }

        if (txtImportDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtImportDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format MM/DD/YYYY");
            return;
        }
        if (txtPrice.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter price");
            return;
        }
        if (txtTax.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter tax");
            return;
        }
        if (txtQuantity.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter Quantity");
            return;
        }
        if (txtDescription.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter Description");
            return;
        }
        Business.Product_BS product = new Business.Product_BS(
                                            txtName.getText(),
                                            txtType.getText(),
                                            txtProductionDate.getText(),
                                            txtExpiryDate.getText(),
                                            txtImportDate.getText(),
                                            Float.parseFloat(txtPrice.getText()),
                                            Float.parseFloat(txtTax.getText()),
                                            Integer.parseInt(txtQuantity.getText()),
                                            Float.parseFloat(txtSellPrice.getText()),
                                            txtDescription.getText()
                                            );

    if(!DAO.Product_DAO.insert(product)) {
        JOptionPane.showMessageDialog(this, "Cannot add this product");
        return;
    }
        this.dispose();
    }//GEN-LAST:event_btCreateActionPerformed

    private void btRefreshOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRefreshOrderActionPerformed
        // TODO add your handling code here:
        refresh();
    }//GEN-LAST:event_btRefreshOrderActionPerformed

    public void refresh(){
        txtName.setText("");
        txtType.setText("");
        txtProductionDate.setText("");
        txtExpiryDate.setText("");
        txtImportDate.setText("");
        txtPrice.setText("");
        txtQuantity.setText("");
        txtDescription.setText("");
    }
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCreate;
    private javax.swing.JButton btExit;
    private javax.swing.JButton btRefresh;
    private javax.swing.JButton btRefreshOrder;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbl;
    private javax.swing.JLabel lblDecription;
    private javax.swing.JLabel lblExpiryDate;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblPrice1;
    private javax.swing.JLabel lblPrice2;
    private javax.swing.JLabel lblPrice3;
    private javax.swing.JLabel lblProductionDate;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JScrollPane txtDscription;
    private javax.swing.JTextField txtExpiryDate;
    private javax.swing.JTextField txtImportDate;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtProductionDate;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtSellPrice;
    private javax.swing.JTextField txtTax;
    private javax.swing.JTextField txtType;
    // End of variables declaration//GEN-END:variables

}
